package com.zut.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.zut.pojo.Business;
import com.zut.pojo.Employee;
import com.zut.pojo.PageInfo;
import com.zut.service.BusinessService;
import com.zut.service.EmployeeService;
import com.zut.service.impl.BusinessServiceImpl;
import com.zut.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author 张晨
 * @date 2024/9/20 18:16
 * @desc
 */
@WebServlet("/business")
public class BusinessController extends HttpServlet {
    private BusinessService businessService = new BusinessServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        if (StringUtils.isEmpty(op)){
            resp.getWriter().write("参数错误");
            return;
        }
        switch (op){
            case "sel":
                sel(req,resp);
                break;
            case "del":
                del(req,resp);
                break;
            case "upd":
                upd(req,resp);
                break;
            case "login":
                login(req,resp);
                break;
            case "register":
                register(req,resp);
                break;
        }
    }

    private void sel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        if (StringUtils.isEmpty(currentPage)){
            currentPage = "1";
        }
        if (StringUtils.isEmpty(pageSize)){
            pageSize = "5";
        }

        String contact = req.getParameter("contact");
        String telephone = req.getParameter("telephone");

        Business business = new Business();

        business.setContact(contact);
        business.setTelephone(telephone);

        PageInfo pageInfo = businessService.conditionSelect(business, Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        String jsonString = JSON.toJSONString(pageInfo);

        resp.getWriter().write(jsonString);
    }
    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Integer count = businessService.deletebusinessById(Integer.parseInt(id));

        resp.getWriter().write(count+"");
    }
    private void upd(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        Integer id = Integer.parseInt(req.getParameter("id"));

        String account = req.getParameter("account");
        String tel = req.getParameter("telephone");
        String contact = req.getParameter("contact");

        Business business = new Business();

        business.setContact(contact);
        business.setTelephone(tel);
        business.setId(id);
        business.setAccount(account);

        Integer count = businessService.updateById(business);

        resp.getWriter().write(count+"");
    }
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String account = req.getParameter("account");
        String password = req.getParameter("password");

        if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
            // 0  用户名密码不能为空
            // 1  用户名不正确
            resp.getWriter().write(0+"");
            return;
        }

        Business business = businessService.login(account,password);

        if (business == null){
            resp.getWriter().write(1+"");
            return;
        }

        String jsonString = JSON.toJSONString(business);
        resp.getWriter().write(jsonString);
    }
    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String telephone = req.getParameter("telephone");
        String contact = req.getParameter("contact");

        Business business = new Business();
        business.setAccount(account);
        business.setPassword(password);
        business.setTelephone(telephone);
        business.setContact(contact);
        business.setRegTime(LocalDateTime.now());
        int result = businessService.register(business);
        resp.getWriter().write(result+"");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
